package com.heima.service.impl;

import com.heima.mapper.OrderMapper;
import com.heima.mapper.ReportMapper;
import com.heima.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ReportMapper reportMapper;
    /**
     * 获取当月订单量
     *
     * @param start
     * @param end
     * @return
     */
    @Override
    public Integer getOrderCountByMonth(String start, String end) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime startTime = LocalDateTime.parse(start, formatter);
        LocalDateTime endTime = LocalDateTime.parse(end, formatter);
        Integer count = reportMapper.getOrderCountByMonth(startTime,endTime);
        return count;
    }

    /**
     * 当月销售额
     *
     * @param start
     * @param end
     * @return
     */
    @Override
    public Integer getOrderAmountByMonth(String start, String end) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime startTime = LocalDateTime.parse(start, formatter);
        LocalDateTime endTime = LocalDateTime.parse(end, formatter);
        Integer amount = reportMapper.getOrderAmountByMonth(startTime,endTime);
        if(amount == null){
            amount = 0;
        }
        return amount;
    }
}
